using System.Collections;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;

public class UI_FADE : MonoBehaviour
{
	public Image FadeImage;

	private static UI_FADE instance;

	private Coroutine currentFadeCoroutine;

	private bool isFading;

	public static bool DarkworldMenu_PreviousState;

	public static bool LightworldMenu_PreviousState;

	public static UI_FADE Instance => instance;

	private void Awake()
	{
		if ((Object)(object)instance != (Object)null && (Object)(object)instance != (Object)(object)this)
		{
			Object.Destroy((Object)(object)((Component)this).gameObject);
		}
		else
		{
			instance = this;
		}
	}

	private void Start()
	{
		//IL_001a: Unknown result type (might be due to invalid IL or missing references)
		((Graphic)FadeImage).color = new Color(0f, 0f, 0f, 1f);
		SceneManager.sceneLoaded += OnSceneLoaded;
	}

	public void StartFadeIn(int GoToScene, float SpeedMultiplier, bool UnpauseOnEnd = false, NewMainMenuManager.MainMenuStates targetmenuState = NewMainMenuManager.MainMenuStates.None)
	{
		if (isFading)
		{
			((MonoBehaviour)this).StopCoroutine(currentFadeCoroutine);
		}
		DarkworldMenu_PreviousState = DarkworldMenu.Instance.CanOpenMenu;
		LightworldMenu_PreviousState = LightworldMenu.Instance.CanOpenMenu;
		DarkworldMenu.Instance.CanOpenMenu = false;
		LightworldMenu.Instance.CanOpenMenu = false;
		((MonoBehaviour)this).StopCoroutine(DelayRevertCMenus());
		currentFadeCoroutine = ((MonoBehaviour)this).StartCoroutine(FadeIn(GoToScene, SpeedMultiplier, targetmenuState, UnpauseOnEnd));
	}

	public void StartFadeOut(float SpeedMultiplier = 1f)
	{
		if (isFading)
		{
			((MonoBehaviour)this).StopCoroutine(currentFadeCoroutine);
		}
		currentFadeCoroutine = ((MonoBehaviour)this).StartCoroutine(FadeOut(SpeedMultiplier));
	}

	private IEnumerator FadeIn(int GoToScene, float SpeedMultiplier, NewMainMenuManager.MainMenuStates targetmenuState, bool UnpauseOnEnd = false)
	{
		isFading = true;
		ChatboxManager.Instance.EndText();
		PlayerManager.Instance._PlayerState = PlayerManager.PlayerState.Cutscene;
		while (((Graphic)FadeImage).color.a < 1f)
		{
			((Graphic)FadeImage).color = new Color(0f, 0f, 0f, ((Graphic)FadeImage).color.a + 0.15f * SpeedMultiplier);
			yield return (object)new WaitForSecondsRealtime(0.01f);
		}
		yield return (object)new WaitForSecondsRealtime(0.1f);
		if (GoToScene > 0)
		{
			SceneManager.LoadScene(GoToScene);
		}
		ChatboxManager.Instance.EndText();
		if (UnpauseOnEnd)
		{
			GonerMenu.Instance.UnpauseGame();
			GonerMenu.Instance.ResetPlayer();
		}
		isFading = false;
		if (targetmenuState != NewMainMenuManager.MainMenuStates.None)
		{
			yield return null;
			while ((Object)(object)NewMainMenuManager.instance == (Object)null)
			{
				yield return null;
			}
			NewMainMenuManager.instance.SetMainMenuState(targetmenuState);
		}
	}

	private IEnumerator FadeOut(float SpeedMultiplier = 1f)
	{
		isFading = true;
		((Graphic)FadeImage).color = new Color(0f, 0f, 0f, 1f);
		while (((Graphic)FadeImage).color.a > 0f)
		{
			((Graphic)FadeImage).color = new Color(0f, 0f, 0f, ((Graphic)FadeImage).color.a - 0.15f * SpeedMultiplier);
			yield return (object)new WaitForSecondsRealtime(0.01f);
		}
		isFading = false;
	}

	private void OnSceneLoaded(Scene scene, LoadSceneMode mode)
	{
		Debug.Log((object)"Reverting CMenus to original states");
		((MonoBehaviour)this).StartCoroutine(DelayRevertCMenus());
	}

	private IEnumerator DelayRevertCMenus()
	{
		yield return (object)new WaitForSeconds(0.25f);
		DarkworldMenu.Instance.CanOpenMenu = DarkworldMenu_PreviousState;
		LightworldMenu.Instance.CanOpenMenu = LightworldMenu_PreviousState;
	}
}
